package com.hais1992.oneday.activity;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hais1992.oneday.R;
import com.hais1992.oneday.common.Config;
import com.hais1992.oneday.common.JsonToObjectList;
import com.hais1992.oneday.dao.ReviewAdapter;
import com.hais1992.oneday.entity.Article;
import com.hais1992.oneday.entity.Review;
import com.hais1992.oneday.entity.User;
import com.hais1992.oneday.listener.UserListener;
import com.hais1992.oneday.tools.GetHttpData;
import com.hais1992.oneday.tools.GetHttpData.GetCallback;
import com.hais1992.oneday.tools.iscroll.XListView;
import com.hais1992.oneday.tools.iscroll.XListView.IXListViewListener;

/**
 * 显示文章详细信息
 * 
 * @author Hello_海生
 * @date 2014年5月26日
 */
public class ShowArticleActivity extends Activity implements IXListViewListener{

	private ImageView userImg; // 右邊的 個人中心
	private ImageView retuenImg; // 左邊的 返回按钮
	private TextView titleTv; // 标题栏

	private TextView articleInfoTv; // 文章信息
	private TextView contentTv; // 文章内容
	private EditText pinglunTv;	//評論
	private ImageView pinglunImg;	//評論按鈕
	
	private XListView listView; // 上啦刷機 List
	private Handler iscrollhandler; // 設置 iscroll 的傳輸
	private List<Map<String, Object>> reviewList = null; // 文章List
	private ReviewAdapter adapter = null; // 文章適配器
	
	private Article article = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.article_show);
	
		userImg = (ImageView) findViewById(R.id.rightImg);
		retuenImg = (ImageView) findViewById(R.id.leftImg);
		titleTv = (TextView) findViewById(R.id.titleTv);
		articleInfoTv = (TextView) findViewById(R.id.articleInfoTv);
		contentTv = (TextView) findViewById(R.id.contentTv);
		pinglunTv = (EditText) findViewById(R.id.pinglunTv);
		pinglunImg = (ImageView) findViewById(R.id.pinglunImg);
		
		String token =Config.getCacheToken(this);
		if(token == null || token.equals("")){
			pinglunTv.setText("必須先登錄！");

			pinglunTv.setCursorVisible(false);
			pinglunTv.setFocusable(false);
			pinglunTv.setFocusableInTouchMode(false);
			
			UserListener userListener = new UserListener();
			pinglunTv.setOnClickListener(userListener);

		}else{
		
			//評論按鈕
			pinglunImg.setOnClickListener(new OnClickListener() {	
				@Override
				public void onClick(View arg0) {
					
					if(pinglunTv.getText()!=null && !pinglunTv.getText().equals("")){
					
						Map<String, String> map = new HashMap<String, String>();
						
						// 发表评论的用户
						User u = new User();
						u.setId(1);
						
						Review r = new Review();
						r.setUserId(u);
						r.setArticleId(article);
						r.setContent(pinglunTv.getText()+"");
						r.setTime(new Date());
						
						map.put("json", JSON.toJSONString(r));
						map.put("key", Config.SERVER_KEY);
						new GetHttpData(Config.SERVER_URL+Config.SAVE_REVIEW_URL, "POST", map, new GetCallback() {
							@Override
							public void onSuccess(String result) {
								JSONObject jsonObject = JSONObject.parseObject(result);
								if(jsonObject.getString("static").equals("error")){
									Toast.makeText(ShowArticleActivity.this, "評論失敗，請輸入内容！", Toast.LENGTH_SHORT).show();
								}else{
									Toast.makeText(ShowArticleActivity.this, "評論成功！", Toast.LENGTH_SHORT).show();
								}
							}
						});
					}else{
						Toast.makeText(ShowArticleActivity.this, "請輸入評論内容！", Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		
		
		
		
		
		//获取数据
		Intent i = getIntent();
		Bundle bundle = i.getBundleExtra("bundle");
		article = (Article) bundle.getSerializable("article");
		
		titleTv.setText(article.getTitle());
		articleInfoTv.setText("分类："+article.getTypeId().getTitle()+"　发布："+article.getUserId().getName()+" 　时间："+article.getInsertTime());
		contentTv.setText(article.getContent());

		// 設置個人中心 菜單按鈕點擊事件
		UserListener userListener = new UserListener();
		userImg.setOnClickListener(userListener);

		// 设置返回键按钮
		retuenImg.setImageDrawable(getResources().getDrawable(R.drawable.left));
		retuenImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ShowArticleActivity.this.finish();
			}
		});
		
		
		
		// 設置上啦刷新
		listView = (XListView) findViewById(R.id.reviewList);
		listView.setPullLoadEnable(true);
		listView.setPullRefreshEnable(false); // 禁用上啦加載
		
		listView.setXListViewListener(this);
		iscrollhandler = new Handler(); // 開始艦艇	
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("start", "0");
		map.put("id", article.getId()+"");
		map.put("count", Config.PAGE_NUNBER);
		map.put("key", Config.SERVER_KEY);

		new GetHttpData(Config.SERVER_URL+Config.GET_REVIEW_URL, "POST", map, new GetCallback() {
			@Override
			public void onSuccess(String result) {
				reviewList= JsonToObjectList.reviewList(result);
				adapter = new ReviewAdapter(ShowArticleActivity.this, reviewList);
				listView.setAdapter(adapter);
			}
		});


	}

	
	
	@Override
	public void onRefresh() {}

	@Override
	public void onLoadMore() {
		iscrollhandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				System.out.println("-------上拉加載 更多---------");
				adapter.notifyDataSetChanged();

				Map<String, String> map = new HashMap<String, String>();
				map.put("start", reviewList.size() + "");
				map.put("count", Config.PAGE_NUNBER);
				map.put("id", article.getId()+"");
				map.put("key", Config.SERVER_KEY);
				

				new GetHttpData(Config.SERVER_URL + Config.GET_REVIEW_URL, "POST",map, new GetCallback() {
							@Override
							public void onSuccess(String result) {
								List<Map<String, Object>> loadList = JsonToObjectList.reviewList(result);
								if (loadList.size() == 0) {
									Toast.makeText(getApplicationContext(),"沒有更老的評論了。。", Toast.LENGTH_SHORT).show();
								} else {
									reviewList.addAll(loadList);
									adapter = new ReviewAdapter(ShowArticleActivity.this, reviewList);
								}
								listView.stopLoadMore(); // 更新完后 恢復狀態
							}
						});

			}
		}, 2000);
	}
}
